package jp.syuriken.snsw.terenaibot.mal.twitter;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

import twitter4j.ResponseList;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;

/**
 * Twitterのユーザー情報を遅延取得します
 * 
 * @author $Author: snswinhaiku.lo@gmail.com $
 */
public class TwitterDelayedUserInfo extends TwitterUserInfo {
	
	public static ArrayList<TwitterDelayedUserInfo> getUserInfos(Twitter twitter,
			TwitterDelayedUserInfo[] twitterDelayedUserInfos) {
		ArrayList<TwitterDelayedUserInfo> userInfos = new ArrayList<TwitterDelayedUserInfo>();
		int arrayLength = twitterDelayedUserInfos.length;
		for (int i = 0; i < arrayLength; i += 20) {
			int count = arrayLength - i;
			count = count > 20 ? 20 : count;
			TwitterDelayedUserInfo[] tempArray = Arrays.copyOfRange(twitterDelayedUserInfos, i, i + count);
			ArrayList<TwitterDelayedUserInfo> tempUserInfos = new ArrayList<TwitterDelayedUserInfo>();
			for (TwitterDelayedUserInfo twitterDelayedUserInfo : tempArray) {
				if (twitterDelayedUserInfo.gotInformation) {
					userInfos.add(twitterDelayedUserInfo);
				} else {
					tempUserInfos.add(twitterDelayedUserInfo);
				}
			}
			long[] tempIds = new long[count];
			int j = 0;
			for (TwitterDelayedUserInfo twitterDelayedUserInfo : tempUserInfos) {
				tempIds[j++] = twitterDelayedUserInfo.userId;
			}
			try {
				ResponseList<User> users = twitter.lookupUsers(tempIds);
				for (User user : users) {
					for (TwitterDelayedUserInfo twitterDelayedUserInfo : tempUserInfos) {
						if (twitterDelayedUserInfo.userId == user.getId()) {
							twitterDelayedUserInfo.setInfo(user);
							userInfos.add(twitterDelayedUserInfo);
							tempUserInfos.remove(twitterDelayedUserInfo);
							break;
						}
					}
				}
				userInfos.addAll(tempUserInfos);
			} catch (TwitterException e) {
				// do nothing
			}
		}
		return userInfos;
	}
	

	protected Twitter twitter;
	
	private TwitterException exception = null;
	

	/**
	 * インスタンスを生成する。
	 * 
	 * @param id ユーザーID
	 * @param twitter Twitterインスタンス。
	 */
	public TwitterDelayedUserInfo(long id, Twitter twitter) {
		userId = id;
		description = null;
		followersCount = -1;
		friendsCount = -1;
		location = null;
		name = null;
		utcOffset = 0;
		url = null;
		timeZone = null;
		screenName = null;
		gotInformation = false;
		this.twitter = twitter;
	}
	
	@Override
	public Date getCreatedAt() {
		getInfo();
		return (Date) createdAt.clone();
	}
	
	@Override
	public String getDescription() {
		getInfo();
		return description;
	}
	
	/**
	 * 情報の取得時に起きたエラーを取得する
	 * 
	 * @return 情報の取得時に起きたエラー
	 */
	public TwitterException getException() {
		return exception;
	}
	
	@Override
	public int getFollowersCount() {
		getInfo();
		return followersCount;
	}
	
	@Override
	public int getFriendsCount() {
		getInfo();
		return friendsCount;
	}
	
	private void getInfo() {
		if (gotInformation == false) {
			User user;
			try {
				user = twitter.showUser(userId);
			} catch (TwitterException e) {
				exception = e;
				return;
			}
			createdAt = user.getCreatedAt();
			description = user.getDescription();
			followersCount = user.getFollowersCount();
			friendsCount = user.getFriendsCount();
			location = user.getLocation();
			name = user.getName();
			utcOffset = user.getUtcOffset();
			url = user.getURL();
			timeZone = user.getTimeZone();
			screenName = user.getScreenName();
		}
	}
	
	@Override
	public String getLocation() {
		getInfo();
		return location;
	}
	
	@Override
	public String getName() {
		getInfo();
		return name;
	}
	
	@Override
	public String getScreenName() {
		getInfo();
		return screenName;
	}
	
	@Override
	public String getTimeZone() {
		getInfo();
		return timeZone;
	}
	
	@Override
	public URL getURL() {
		getInfo();
		return url;
	}
	
	@Override
	public String getUserId() {
		return String.valueOf(userId);
	}
	
	@Override
	public int getUtcOffset() {
		getInfo();
		return utcOffset;
	}
	
}
